<?php
/**
 * 商品分类 model 类
 *
 * @author <judasnow@gmail.com>
 */
require_once( APPPATH . 'models/base_model.php' );

class product_class_model extends base_model{
	
	private $_table_name;

	public function __construct(){
		$this->_table_name = 'product_class';
		parent::__construct( $this->_table_name );
	}
	 
	public function add_new( $new_product_class_info ){
		//去除 new_class_describe 
		unset( $new_product_class_info['new_class_describe'] );
		parent::add_new( $new_product_class_info );
	}

	//重载 find_all 函数
	//根据 order 进行显示的排序
	//@param bool $is_show 为 TRUE 则只输出 show 字段为 1 的记录
	public function find_all( $is_show = FALSE ){
		 if( $is_show ){
			  $this->db->where( 'show' , 1 );
		 }
		 $this->db->order_by( 'order' , 'ASC' );
		 $res = $this->db->get( $this->_table_name );	
		 return $res->result_array();
	}

	//根据分类编号获取分类的名称
	public function get_class_name_by_id( $class_id ){
		$this->db->where( 'class_id' , $class_id );
		$q = $this->db->get( 'product_class' );
                $res = $q->result_array();
                if( $q->num_rows() != 1 ){
			//throw new Exception( '尝试查询了不存在的商品分类,或者商品分类信息异常 SQL=[' . $this->db->last_query() . ']' );
			$res[0]['product_class_name_cn'] = '[该服务已删除]';
		}
		return $res[0]['product_class_name_cn'];
	}

	public function get_class_id_by_name_cn( $class_name_cn ){
                $this->db->where( 'product_class_name_cn' , $class_name_cn );
                $q = $this->db->get( 'product_class' );
                $res = $q->result_array();
                if( $q->num_rows() != 1 ){
                        //throw new Exception( '尝试查询了不存在的商品分类,或者商品分类信息异常' );
                        $res[0]['class_id'] = -1;
                }
                return $res[0]['class_id'];
        }
}
